'use strict'
/**
 * 
 * 该文件中的接口为后台管理系统使用
 */
class contact {
    constructor () {
        
    }
    // __mid(){// 启用token-verify中间件
    //     return [
    //       {
    //         name:'token-verify',
    //         path:['list','get','post','delete']
    //       }
    //     ]
    // }
    async list(c) { //查看所有建议列表
        let sqltext = `SELECT * FROM suggestion`;
        let r = await c.service.psql.query(sqltext,[]);
        console.log(r.rows);
        c.send(JSON.stringify(r.rows));
    }
    async get(c){ //查看单个建议信息
        let sqltext = `SELECT * FROM suggestion WHERE "suggestionId"=$1`;
        let r = await c.service.psql.query(sqltext,[c.param.id]);
        console.log(r.rows);
        c.send(JSON.stringify(r.rows));
    }
    async post(c){ //修改建议状态
        try {
            let obj = JSON.parse(c.body);
            let status = obj.status;
            console.log('obj：',obj);
            console.log("status：",status)
            let sqltext = `UPDATE suggestion SET "suggestionStatus"=$1 WHERE "suggestionId"=$2`;
            let r = await c.service.psql.query(sqltext,[status,obj.id])
            if (r.rowCount != 0) { //对是否提交成功做判断
                c.send('Update suggestion success!')
            } else {
                c.status(500).send('Update suggestion failed!')
            }
        } catch (error) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async delete(c){ //删除单个建议
        try {
            let sqltext = `DELETE FROM suggestion WHERE "suggestionId"=$1`; 
            let r = await c.service.psql.query(sqltext,[c.param.id])
            if (r.rowCount != 0) { //对是否删除成功做判断
                c.send('Delete suggestion success!')
            } else {
                c.status(500).send('Delete suggestion failed!')
            }
        } catch (error) {
            c.status(500).send("Database's operations failed!")
        }
    }
}

module.exports = contact;